// component/mCard/index.ts
Component({
  options: {
    multipleSlots: true
  },

  /**
   * 组件的属性列表
   */
  properties: {
    // 标题
    title: {
      type: String,
      value: ''
    },
    // 是否显示展开按钮
    showExpand: {
      type: Boolean,
      value: true
    },
    expand: {
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isAnim: false,
    expand2: true, // 展开
    height: ''
  },

  lifetimes: {
    attached: function() {
      let obj = {
        expand2: this.properties.expand
      }
      if(!this.properties.expand) {
        obj.height = '0';
      }
      this.setData(obj);
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取当前卡片的高度
    getCtHeight() {
      return new Promise((resolve:any, reject:any) => {
        const query = wx.createSelectorQuery().in(this);
        query.select('#downInfoContent .show').boundingClientRect((rect:any) => {
          if (rect) {
            const height = rect.height;
            resolve(height+'px');
          } else {
            reject();
          }
        }).exec();
      })
    },

    // 切换状态
    async toggleExpand() {
      if(this.data.isAnim) {
        return;
      }
      this.setData({
        isAnim: true
      })
      if(this.data.expand2) {
        let height = await this.getCtHeight();
        this.setData({
          height,
          expand2: false
        })
        setTimeout(() => {
          this.setData({
            height: 0
          })
          // 设置未使用动画
          setTimeout(() => {
            this.setData({
              isAnim: false
            })
          }, 300);
        }, 50);
      } else {
        let height = await this.getCtHeight();
        this.setData({
          height,
          expand2: true
        })
        setTimeout(() => {
          this.setData({
            height: '',
            isAnim: false
          })
        }, 300);
      }
    }
  }
})
